//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Components;
using CommunityServer;

namespace CommunityServerWeb.ControlPanel.Tools.Reports
{
	/// <summary>
	/// Summary description for PollVotingReport.
	/// </summary>
	public class PollVotingReport: BaseToolsPage
	{
		protected Literal Title;
		protected HyperLink DownloadCSV;

		protected Post post;
		protected PollSummary poll;

		private void Page_Load(object sender, System.EventArgs e)
		{
			Title.Text = String.Format(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Tools_PollVotingReport_Title"), post == null ? "?" : post.Subject);
			DownloadCSV.Text = CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Tools_PollVotingReport_DownloadCSV");
			DownloadCSV.NavigateUrl = "PollVotingReport.aspx?pid=" + Request.QueryString["pid"] + "&atid=" + Request.QueryString["atid"] + "&export=csv";

			if (Request.QueryString["export"] != null && !IsPostBack && !IsCallBack)
				GenerateExport();
		}

		private void GenerateExport()
		{
			if (poll == null)
				return;

			PollVoteSet pvs = poll.GetUserVotes(0, int.MaxValue - 1); 
			Response.Clear();
			Response.Cache.SetLastModified(DateTime.Now);
			Response.ContentType = "text/csv; name=\"VotingHistory.csv\"";
			Response.AddHeader("Content-disposition", "inline; filename=\"VotingHistory.csv\"");

			foreach (PollVote pv in pvs.PollVotes)
			{
				Response.Write(String.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"\n", 
					pv.User.Username.Replace("\"", "\"\""), 
					pv.User.DisplayName.Replace("\"", "\"\""), 
					pv.User.Email.Replace("\"", "\"\""), 
					pv.Answer.Replace("\"", "\"\"")));
			}

			Response.Flush();
			Response.End();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			CSContext cntx = CSContext.Current;
			int postID = cntx.GetIntFromQueryString("pid",-1);
			int applicationTypeID = cntx.GetIntFromQueryString("atid",-1);

			if (postID != -1 && applicationTypeID != -1)
			{
				CSApplicationData appData = ApplicationSet.Applications[(ApplicationType) applicationTypeID];
				if (appData != null)
				{
					post = appData.GetPost(postID);
					if (post != null)
						poll = Polls.GetPoll(post);
				}
			}

			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
